﻿using Mengsk.ProxyDelivery.Service;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Mengsk.ProxyDelivery.App.UI.Goods
{
    /// <summary>
    /// Interaction logic for GoodsDuplicateCleanUserControl.xaml
    /// </summary>
    public partial class GoodsDuplicateCleanUserControl : UserControl, IPlugin
    {
        public GoodsDuplicateCleanUserControl()
        {
            InitializeComponent();
        }

        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var vendors = ServiceContainer.GetService<VendorService>().GetAll();
                Dictionary<string, int> homePages = new Dictionary<string, int>();
                foreach (var vendor in vendors.Datas)
                {
                    if (homePages.ContainsKey(vendor.HomePage.Trim()) == false)
                    {
                        homePages[vendor.HomePage.Trim()] = 1;
                    }
                    else
                    {
                        homePages[vendor.HomePage.Trim()]++;
                    }
                }

                var vens = vendors.Datas.Where(obj => homePages[obj.HomePage.Trim()] > 1).OrderBy(obj => obj.Id).OrderBy(obj => obj.HomePage.Trim()).ToArray();
                this.dgvVendors.ItemsSource = vens;
                this.tbTotal.Text = "查询到总数:" + vens.Count();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnProcess_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var item = this.dgvVendors.SelectedItem as Mengsk.ProxyDelivery.Domain.Vendor;
                if (item == null)
                {
                    throw new Exception("请选择厂家");
                }

                if (MessageBox.Show("确认删除该厂家商品及厂家", "警告", MessageBoxButton.YesNo, MessageBoxImage.Error) != MessageBoxResult.Yes)
                {
                    return;
                }
                var shoes = ServiceContainer.GetService<ShoesService>().GetAllByField("vendorId", item.Id, 0, 0);
                int count = 1;
                foreach (var s in shoes.Datas)
                {
                    ServiceContainer.GetService<ShoesService>().Delete(s.Id);
                    this.tbMessage.Text = "正在删除:" + count + "/" + shoes.Datas.Count;
                    count++;
                    App.DoEvents();
                }
                ServiceContainer.GetService<VendorService>().Delete(item.Id);
                MessageBox.Show("已完成");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public string Title
        {
            get { return "重复厂家清理"; }
        }

        public Control UIControl
        {
            get { return this; }
        }


        private void Open_Click(object sender, EventArgs e)
        {
            try
            {
                TextBlock tb = sender as TextBlock;
                if (tb == null)
                {
                    throw new Exception("Edit_Click错误，事件源不是TextBlock");
                }
                Mengsk.ProxyDelivery.Domain.Vendor vendor = tb.DataContext as Mengsk.ProxyDelivery.Domain.Vendor;
                Process.Start(vendor.HomePage.Trim());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnProcessAll_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var vendors = this.dgvVendors.ItemsSource as Mengsk.ProxyDelivery.Domain.Vendor[];

                if (vendors == null || vendors.Length < 1)
                {
                    throw new Exception("没有数据处理");
                }
                //根据homepage分组
                var venGroup = vendors.GroupBy(obj => obj.HomePage.Trim()).ToArray();

                foreach (var vs in venGroup)
                {
                    //根据id升序
                    var vens = vs.OrderBy(obj => obj.Id).ToArray();

                    foreach (var v in vens)
                    {
                        if (v == vens.Last())
                        {
                            continue;
                        }
                        this.dgvVendors.SelectedItems.Clear();
                        this.dgvVendors.SelectedItem = v;
                        this.dgvVendors.ScrollIntoView(v);
                        App.DoEvents();
                        //
                        var shoes = ServiceContainer.GetService<ShoesService>().GetAllByField("vendorId", v.Id, 0, 0);
                        int count = 1;
                        foreach (var s in shoes.Datas)
                        {
                            ServiceContainer.GetService<ShoesService>().Delete(s.Id);
                            this.tbMessage.Text = "正在删除:" + v.Name + " " + count + "/" + shoes.Datas.Count;
                            count++;
                            App.DoEvents();
                        }
                        ServiceContainer.GetService<VendorService>().Delete(v.Id);
                    }
                }
                MessageBox.Show("已完成");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
